package com.ws.service.impl;

import com.ws.common.CommonConstant;
import com.ws.common.PaasPage;
import com.ws.entity.Book;
import com.ws.repository.BookRepository;
import com.ws.service.BookService;
import com.ws.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 9:50
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Service
public class BookServiceImpl implements BookService {
    @Autowired
    BookRepository bookRepository;

    @Override
    public PaasPage<Book> getBookPage(Pageable pageable, String bookName, String author, String publisher,String statusCd) {
        PageUtil.setPageAndOrderBy(pageable);
        Book book = new Book();
        book.setBookName(bookName);
        book.setAuthor(author);
        book.setPublisher(publisher);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        book.setStatusCd(statusCd);
        Page<Book> bookPage = bookRepository.getBookPage(book);
        return PageUtil.transformPage(bookPage);
    }

    @Override
    public Book saveBook(Book book) {
        if(null == book.getBookId()){
            book.setCreateTime(new Date());
            book.setStatusCd(CommonConstant.ADD_STATUS_CD);
            bookRepository.saveBook(book);
        }else{
            bookRepository.updateBook(book);
            return book;
        }

        return book;
    }

    @Override
    public Book getBookBasicInfo(Long bookId) {
        return bookRepository.getBookBasicInfo(bookId);
    }

    @Override
    public void delBook(Long bookId) {
        bookRepository.delBook(bookId);
    }

    @Override
    public Book updateBook(Long bookId, Book book) {
        bookRepository.updateBook(book);
        return book;
    }

    @Override
    public List<Book> getBookList(String bookName, String author, String publisher, String statusCd) {
        Book book = new Book();
        book.setBookName(bookName);
        book.setAuthor(author);
        book.setPublisher(publisher);
        if(StringUtils.isEmpty(statusCd)){
            statusCd = CommonConstant.ADD_STATUS_CD;
        }
        book.setStatusCd(statusCd);
        List<Book> books = bookRepository.getBookList(book);
        return books;
    }
}
